<?php

class Setting extends CActiveRecord
{
	private static $_items=array();

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{setting}}';
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('code, value,', 'length', 'max'=>50),
			array('name, value,', 'length', 'max'=>100),
			array('description', 'length', 'max'=>255),
			array('create_time, update_time', 'unsafe'),
		);
	}
	
	/**
	 * Returns the items for the specified type.
	 * @param string item type (e.g. 'period').
	 * @return item names indexed by item code.
	 * An empty value is returned if the item type does not exist.
	 */
	public static function get($type)
	{
		if(!isset(self::$_items[$type]))
			self::loadItems();
		return self::$_items[$type];
	}
	
	/**
	 * Loads items from the database.
	 * @param string the item type
	 */
	private static function loadItems()
	{
		self::$_items=array();						//FIXME Сделать критерию с селект где не будет лишних полей
		$models=self::model()->findAll();
		foreach($models as $model)
			self::$_items[$model->code]=$model->value;
	}
	
	public function behaviors()
    {
        return array(
			'History' => array(
                'class'=>'application.components.behaviors.HistoryBehavior',
            ),
			'OldAttributes' => array(
				'class'=>'application.components.behaviors.OldAttributesBehavior',
			),
        );
    }
}